<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
              <a-input :placeholder="$t('m.bpm.other.qing-shu-ru-jiao-se-ming-cheng-cha-xun')" v-model="queryParam.name"></a-input>
          </a-col>
          <a-col :md="12" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">{{ $t('m.common.button.query') }}</a-button>
                <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: computedKeys, onChange: onSelectChange}"
      @change="handleTableChange">
    </a-table>
  </a-card>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SelectRoleList',
    props: {
      value: {
        type: Array,
        required: true
      }
    },
    mixins: [YcListMixin],
    data () {
      return {
        columns: [
          {
            title: this.$t('m.system.role.entity.roleName'),
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: this.$t('m.system.role.entity.roleCode'),
            align: 'center',
            dataIndex: 'roleCode'
          },
        ],
        url: {
          list: YchConstant.PREFIX_BPM + '/bpm-back/processSelectRest/getRoleListByPage/v1',
        },
      }
    },
    computed: {
      computedKeys () {
        return this.value.map(item => item.id)
      }
    },
    methods: {
      onSelectChange (selectedRowKeys, selectedRows) {
        const oldKeys = this.computedKeys
        const data = this.value.filter(item => selectedRowKeys.indexOf(item.id) != -1)//原来剩下的
        const addIds = selectedRowKeys.filter(id => oldKeys.indexOf(id) == -1)//新增的
        selectedRows.forEach(item => {
          if (addIds.indexOf(item.id) != -1) {
            data.push({
              id: item.id,
              name: item.name
            })
          }
        })
        this.$emit('input', data)
      }
    }
  }
</script>
